using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(PathNode))]

public class PathNodeEditor : Editor {
	
	Rect r;
	
	override public void OnInspectorGUI() {
		DrawDefaultInspector();
    	r = EditorGUILayout.BeginHorizontal("Button");
        if (GUI.Button (r, GUIContent.none))
		{
			LinkAdjacentPahtNode();
		}
		
    	GUILayout.Label ("Link Adjacent Path node");
        EditorGUILayout.EndHorizontal();
		
		
		r = EditorGUILayout.BeginHorizontal("Button");
        if (GUI.Button (r, GUIContent.none))
		{
			DuplicateNewPathNode();
		}
   	 	GUILayout.Label ("Dulplicate new PathNode");
        EditorGUILayout.EndHorizontal();
		
		
		r = EditorGUILayout.BeginHorizontal("Button");
        if (GUI.Button (r, GUIContent.none))
		{
			DuplicateNewPathNode();
		}
   	 	GUILayout.Label ("Link PathNode");
        EditorGUILayout.EndHorizontal();
	}
	
	void OnEnable() {
		PathNode p = (PathNode)target;
		
		p.trans = p.transform;
	//	Debug.Log(GameObject.FindWithTag("PathNodes").GetComponent<PathNodeContainer>());
		if (p.container == null) {
			p.container = GameObject.FindWithTag("PathNodes").GetComponent<PathNodeContainer>();
		}
	}
	
	void DuplicateNewPathNode() {
		GameObject dup = new GameObject();
		dup.AddComponent("PathNode");
		dup.transform.position = ((PathNode)target).gameObject.transform.position;
		PathNode p = (PathNode)target;
		int pnNum = p.container.GetComponentsInChildren<PathNode>().Length;
		pnNum++;
		if (pnNum < 10) {
			dup.name = "pn0" + pnNum;	
		} else {
			dup.name = "pn" + pnNum;	
		}
	}
	
	void LinkAdjacentPahtNode() {
		PathNodeContainer pnc = GameObject.FindWithTag("PathNodes").GetComponent<PathNodeContainer>();
		pnc.LinkAdjacentNode();
//		PathNode[] pn = p.gameObject.gameObject.GetComponentsInChildren<PathNode>();
//		Debug.Log(p.gameObject);
	}

}
